# -*- coding: utf-8 -*-
from django.shortcuts import render
from django.contrib.auth.decorators import login_required
from advertising_app.forms import AuthorCreationForm, AdvertisementSearchForm, NewAdvertisementForm
from django.http import HttpResponseRedirect
from advertising_app.models import Advertisement, Offer

# Create your views here.

def home_view(request):
    return render(request,"main_page.html")

def register_view(request):
    if request.method == 'POST':
        form = AuthorCreationForm(request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect("/accounts/register/complete/")
    else:
        form = AuthorCreationForm()
    return render(request,"registration/register.html", {'form': form})

def register_complete_view(request):
    return render(request,"registration/register_complete.html")

def logged_out_view(request):
    return render(request,"registration/logged_out.html")

@login_required
def add_new_advertisement_view(request):
    if request.method == 'POST':
        form = NewAdvertisementForm(request.POST)
        if form.is_valid():
            cd = form.cleaned_data
            new_advert = Advertisement(city = cd['city'], 
                                       street = cd['street'], 
                                       house_number = cd['house_number'], 
                                       description = cd['description'], 
                                       show_contact_data = cd['show_contact_data'])
            new_advert.author = request.user.author
            new_advert.save()
            return HttpResponseRedirect('/')
        else:
            return render(request, "add_new_advertisement_form.html", {'form': form})
    else:
        form = NewAdvertisementForm()
        return render(request, "add_new_advertisement_form.html", {'form': form})
    
@login_required
def search_view(request):
    if request.method == 'POST':
        form = AdvertisementSearchForm(request.POST)
        if form.is_valid():
            cd = form.cleaned_data
            adverts = Advertisement.objects.filter(city__contains=cd['city'], street__contains=cd['street'])
            return render(request, "search.html", {'adverts': adverts })
        else:
            return render(request, "search_advertisements_form.html", {'form': form})
    else:
        form = AdvertisementSearchForm()
        return render(request, "search_advertisements_form.html", {'form': form})
        
@login_required
def detailed_advertisement_view(request, ad_id):
    ad = Advertisement.objects.filter(id=int(ad_id))
    return render(request, "detailed_advertisement.html", {'ad': ad[0] })

@login_required
def offers_view(request):
    offers = Offer.objects.all()
    return render(request, "offers.html", {'offers': offers })

@login_required
def advertisements_view(request):
    adverts = Advertisement.objects.all()
    return render(request, "advertisements.html", {'adverts': adverts })

def contact_view(request):
    return render(request,"contact.html")

@login_required
def profile_view(request):
    user = request.user
    author = None
    if hasattr(user,'author'):
        author=user.author
    return render(request,"profile.html",{'user':user,'author':author})